package com.example.viewpager_fragment.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import com.example.viewpager_fragment.R

class FirstFragment : Fragment() {

    private val TAG = "FirstFragment"

    lateinit var btnFragment_F1: Button
    lateinit var btnFragment_F2: Button
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.i(TAG, "onCreateView: ")
        //  Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btnFragment_F1 = view.findViewById(R.id.btn_F1)
        btnFragment_F2 = view.findViewById(R.id.btn_f2)

        // 初始化默认显示的Fragment
        if (savedInstanceState == null) {
            childFragmentManager.beginTransaction()
                .replace(R.id.containerFrag, FirstFragment_1())
                .commit()
        }

        // 按钮点击事件切换Fragment
        btnFragment_F1.setOnClickListener {
            childFragmentManager.beginTransaction()
                .replace(R.id.containerFrag, FirstFragment_1())
                .addToBackStack(null) // 添加到返回栈
                .commitAllowingStateLoss()
        }

        btnFragment_F2.setOnClickListener {
            childFragmentManager.beginTransaction()
                .replace(R.id.containerFrag, FirstFragment_2())
                .addToBackStack(null) // 添加到返回栈
                .commitAllowingStateLoss()
        }
    }

    override fun onResume() {
        Log.i(TAG, "onResume: ")
        super.onResume()
    }

    override fun onAttach(context: Context) {
        Log.i(TAG, "onAttach: ")
        super.onAttach(context)
    }

    override fun onDestroy() {
        Log.i(TAG, "onDestroy: ")
        super.onDestroy()
    }

    override fun onPause() {
        Log.i(TAG, "onPause: ")
        super.onPause()
    }
    
}
